
package editeur;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.*;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("unused")
public class Album implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 965264620660034601L;
	String nom;
	boolean saved=false;
	private Vector<Photo> album=new Vector<Photo>();
	private JButton jb;
	private int indice;
	
	//getters and setters
	public int getIndice() {
		return indice;
	}
	public void setIndice(int indice) {
		this.indice = indice;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public boolean isSaved() {
		return saved;
	}

	public void setSaved(boolean saved) {
		this.saved = saved;
	}

	public Vector<Photo> getAlbum() {
		return album;
	}

	public void setAlbum(Vector<Photo> album) {
		this.album = album;
	}
	
	//constructeurs
	public Album(){
		album=new Vector<Photo>();
		indice=0;
    }
	public Album(String n){
		album=new Vector<Photo>();
		nom=n;
		indice=0;
    }
	
	//Methodes
	public boolean AddPhoto( Photo p){
		if(album.contains(p))
		{
			JOptionPane.showMessageDialog(null,"Photo déjà présente");
			return false;
		}
		album.add(p);
		//JOptionPane.showMessageDialog(null,"Photo ajoutée");
		indice++;
		return true;
		
	
	}
	
	public boolean RemovePhoto(Photo p){
		if(album.contains(p))
		{
			JOptionPane.showMessageDialog(null,"Photo supprimée");
			return album.remove(p);
		}
		else
		{
			JOptionPane.showMessageDialog(null,"Photo Inexistante");
			return false;
		}
			
	}
	
	public boolean  Save(JFrame f) throws IOException{
        if(this.saved==true)
        {
                JOptionPane.showMessageDialog(null,"Fichier à jour");
                return saved;
        }
        int confirm=0;
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File file=null;
do{
        int returnVal = fc.showSaveDialog(f);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
        file = fc.getSelectedFile();
        
        if(file.exists())
        {
                confirm=JOptionPane.showConfirmDialog(null, "Ce fichier existe. Voulez vous l'écraser");
                if(confirm==JOptionPane.YES_OPTION)
                {               
                        file.mkdir();
                        for(Photo p:album){
                        ImageIO.write(p,"jpg",new File(file.getAbsolutePath()+System.getProperty("file.separator")+p.getNom()));
                        }
                        saved=true;
                        JOptionPane.showMessageDialog(null, "Fichier bien sauvegardé");
                
                }   
        
        }
        else{
                file.mkdir();
                for(Photo p:album){
                ImageIO.write(p,"jpg",new File(file.getAbsolutePath()+System.getProperty("file.separator")+p.getNom()));
                }
                saved=true;
                JOptionPane.showMessageDialog(null, "Fichier bien sauvegardé");
        }
    }
    else if(returnVal==JFileChooser.CANCEL_OPTION)
    {
        return false;
    }
   }while(saved==false);
    return saved;


        
}
	
    public void afficheAlbum(){
    	
    	System.out.println("Album courant : "+this.getNom());
    	System.out.println("Nombre de photos : "+this.album.size());
    	for(Photo p:album){
    		
    		System.out.println(p.getNom());
    	}
    }
	/*
	public static void main(String args[]) throws Exception{
		JFrame j=new JFrame();
		j.setSize(800, 600);
		j.setLocationRelativeTo(null);
		j.setResizable(false);
		j.setTitle("Test");
		j.setVisible(true);
		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		j.setForeground(Color.BLACK);
		Graphics2D g=(Graphics2D)j.getGraphics();	
		 String ref="";
	        JFileChooser fc = new JFileChooser();
	        File file=null;
	        int returnVal=fc.showOpenDialog(null);
	        if (returnVal == JFileChooser.APPROVE_OPTION)
	        {
	        	file = fc.getSelectedFile();
	        	ref=file.getAbsolutePath();
	        }
		BufferedImage img=ImageIO.read(new File(ref));
		Photo p=new Photo("p",img);
		//Photo p2=new Photo("p2");
		//Photo p3=new Photo("p3");
		
		
	
		Album a=new Album();
		a.AddPhoto(p);
		//a.AddPhoto(p3);
		//a.AddPhoto(p2);
		a.Save(j);
		//a.Load(j);
		g.drawImage(a.album.elementAt(0),null,0,0);
		a.afficherContenu();
		//a.RemovePhoto(p);
		
	}*/
}

